
import * as request from "./request.js";
let storage = window.localStorage;
if (!storage) {
	console.log('浏览器版本太低，不支持localStorage');
}
function set_value(elementId, msg)
{
  var parser = new DOMParser();
  var xmlDoc = parser.parseFromString(msg, "application/xml");
  var root = xmlDoc.documentElement;
  var child = root.childNodes[0];
  var value;
  value = child.getAttribute("error");
  if (value) {
    document.getElementById("notice").innerText =
      '[ERROR] :' + value;
  } else {
    value = child.getAttribute("verification");
    var e = document.getElementById(elementId);
    e.value = value;
  }
}
function verification(){
	var email = document.getElementById('email');
  var numberPattern = /^\d+$/;
  if (!numberPattern.test(email.value)) {
    alert("请输入纯数字！");
    return false; // 阻止表单提交
  }
  var requestStr = protocol_encode("option", "verification",
    "email", email.value + "@qq.com", "varification", "",
    "usr", "", "pwd", "");
  request.post_callback("account", requestStr,
    "varification", set_value);
}
function storage_save(name, msg)
{
  var parser = new DOMParser();
  var xmlDoc = parser.parseFromString(msg, "application/xml");
  var root = xmlDoc.documentElement;
  var child = root.childNodes[0];
  var value;
  value = child.getAttribute("error");
  if (value) {
    document.getElementById("notice").innerText =
      '[ERROR] :' + value;
  } else {
    value = child.getAttribute(name);
    console.log("get ", name, ":", value);
    document.getElementById("notice").innerHTML = "token:" + value;
    storage.token = value;
    var jump;
    if (storage.previous && (storage.previous != "sign_up.html")) {
      jump = storage.previous;
      top.window.location.href=jump + "?token=" +
        storage.token + "&id=" + storage.id;
       //   window.history.back();
        //  top.window.location.href="http://bef.gitee.io";
    }
  }
}
function sign_up(){
	let storage = window.localStorage;
	var verification = document.getElementById('verification');
  var numberPattern = /^\d+$/;
  if (!numberPattern.test(verification.value)) {
    alert("verification 请输入纯数字！");
    return false; // 阻止表单提交
  }
	var email = document.getElementById('email');
  if (!numberPattern.test(email.value)) {
    alert("email 请输入纯数字！");
    return false; // 阻止表单提交
  }
	var usr = document.getElementById('usr');
  if (usr.value.trim() === "") {
    alert("请输入昵称！");
    return false; // 阻止表单提交
  }
	var pwd = document.getElementById('pwd');
  if (pwd.value.trim() === "") {
    alert("请输入密码！");
    return false; // 阻止表单提交
  }
	var confirm_pwd = document.getElementById('confirm_pwd');
  confirm_pwd.disable = true;
	if (pwd.value != confirm_pwd.value) {
		console.log("pwd:" + pwd.value + "confirm pwd:" +
      confirm_pwd.value);
		document.getElementById('error').innerHTML = "错误：两次密码不一样";
	} else if (usr.value == "") {
		document.getElementById('error').innerHTML = "错误：昵称不能为空";
	} else {
    var requestStr = protocol_encode("option", "signIn",
      "email", "", "varification", "",
      "usr", usr.value, "pwd", pwd.value);
    request.post_callback("account", requestStr,
      "token", storage_save, true);
	}
}
function protocol_xml_encode(name0, value0, name1, value1) {
  var element = xmlDoc.createElement('child');
  element.setAttribute('id', usr.value);
  element.setAttribute('pwd', pwd.value);
  if (typeof storage !== 'undefined')
    element.setAttribute('token', storage.token);
  xmlDoc.documentElement.appendChild(element);
  var xmlString = new XMLSerializer().serializeToString(xmlDoc);
  return xmlString;
}
function protocol_encode(...args) {
  const params = new URLSearchParams();
  for (let i = 0; i < args.length; i += 2) {
    if (args[i + 1] !== undefined) {
        //console.log(args[i], args[i + 1]);
        params.append(args[i], args[i + 1]);
    } else {
        console.log(args[i]);
    }
  }
  return params.toString();
}
function sign_in() {
  event.preventDefault();
	var usr = document.getElementById('usr');
	var pwd = document.getElementById('pwd');
	var sign = document.getElementById('sign_in');
  var xmlDoc = document.implementation.createDocument(null, 'root');
  //var requestStr = protocol_xml_encode(
  var requestStr = protocol_encode("option", "signIn",
    "email", "", "varification", "",
    "usr", usr.value, "pwd", pwd.value);
  if (typeof storage !== 'undefined')
    storage.id = usr.value;
  request.post_callback("account", requestStr,
    "token", storage_save);
}
export function sign_up_form(containerId) {
  storage.nowHref = "signUp";
  const container = document.getElementById(containerId);
  container.innerHTML = "";
  //container.style.textAlign = 'center';
  var input, label, br;
  var a = document.createElement("a");
  a.style.float = 'right';
  a.href = "sign_in.html";
  a.innerHTML = "sign in";
  container.appendChild(a);
  br = document.createElement("br");
  container.appendChild(br);
  const form = document.createElement("form");
  form.style.textAlign = 'center';
  container.appendChild(form);
  form.action = "/account";
  form.method = "POST";
  form.id = 'form';
  input = document.createElement("input");
  input.type = "hidden";
  input.id = "option";
  input.value = "signUp";
  form.appendChild(input);
  label = document.createElement("label");
  label.setAttribute("for", "email");
  label.textContent = "email";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "email";
  //input.value = "210743063@qq.com";
  input.value = "210743063";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  label = document.createElement("label");
  label.setAttribute("for", "verification");
  label.textContent = "verification";
  form.appendChild(label);
  var button = document.createElement("button");
  button.setAttribute("type", "button");
  button.setAttribute("value", "get");
  button.textContent = "get";
  button.onclick = function() {
    verification();
  }
  form.appendChild(button);
  br = document.createElement("br");
  form.appendChild(br);

  input = document.createElement("input");
  input.type = "text";
  input.id = "verification";
  input.value = "56933";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  label = document.createElement("label");
  label.setAttribute("for", "usr");
  label.textContent = "usr";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "usr";
  input.value = "8";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  label = document.createElement("label");
  label.setAttribute("for", "pwd");
  label.textContent = "pwd";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "pwd";
  input.value = "3233";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  label = document.createElement("label");
  label.setAttribute("for", "confirm_pwd");
  label.textContent = "confirm pwd";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "confirm_pwd";
  input.value = "3233";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "submit";
  input.id = "signIn";
  input.value = "submit";
  form.appendChild(input);
  form.addEventListener('submit', function(event) {
    event.preventDefault();
    sign_up();
  });
}
export function sign_in_form(containerId) {
  storage.nowHref = "signIn";
  const container = document.getElementById(containerId);
  container.innerHTML = "";
  //container.style.textAlign = 'center';
  var input, label, br;
  var a = document.createElement("a");
  a.style.float = 'right';
  a.href = "sign_up.html";
  a.innerHTML = "sign up";
  a.onclick = function() {
    sign_up_form(containerId);
    return false;
  }
  container.appendChild(a);
  br = document.createElement("br");
  container.appendChild(br);
  const form = document.createElement("form");
  form.style.textAlign = 'center';
  container.appendChild(form);
  form.action = "/account";
  form.method = "POST";
  form.id = 'form';
  input = document.createElement("input");
  input.type = "hidden";
  input.id = "option";
  input.value = "signIn";
  form.appendChild(input);
  input = document.createElement("input");
  input.type = "hidden";
  input.id = "email";
  input.value = "";
  form.appendChild(input);
  input = document.createElement("input");
  input.type = "hidden";
  input.id = "verification";
  input.value = "";
  form.appendChild(input);
  label = document.createElement("label");
  label.setAttribute("for", "usr");
  label.textContent = "usr";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "usr";
  input.value = "8";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  label = document.createElement("label");
  label.setAttribute("for", "pwd");
  label.textContent = "pwd";
  form.appendChild(label);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "text";
  input.id = "pwd";
  input.value = "3233";
  form.appendChild(input);
  br = document.createElement("br");
  form.appendChild(br);
  input = document.createElement("input");
  input.type = "submit";
  input.id = "signIn";
  input.value = "submit";
  form.appendChild(input);
  form.addEventListener('submit', function(event) {
    event.preventDefault();
    sign_in();
  });
}
/*
  document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('signIn').addEventListener('click',
      signIn.sign_in);
  });
  */

